<template>
  <!-- 最外层的容器 -->
  <view class="my-settle-container">
    <!-- 全选区域 -->
    <label class="radio" @click="controlRadio">
      <radio color="#f36414" :checked="isAllSelected" /><text>全选</text>
    </label>

    <!-- 合计区域 -->
    <view class="amount-box">
      合计:<text class="amount">￥{{getGoodsPrice}}</text>
    </view>

    <!-- 结算按钮 -->
    <view class="btn-settle" @click="settelment">结算({{getChecked}})</view>
  </view>

</template>

<script>
  import {
    mapGetters,
    mapMutations,
    mapState
  } from 'vuex'
  export default {
    data() {
      return {
        seconds: 3,
        timer: null
      };
    },
    computed: {
      ...mapGetters('myUser', ['addressStr']),
      ...mapGetters('mycart', ['getChecked', 'getTotal', 'getGoodsPrice']),
      ...mapState('myUser', ['token']),
      ...mapState('mycart', ['cart']),
      isAllSelected() {
        // console.log(this.getChecked === this.getTotal)
        return this.getChecked === this.getTotal
      }
    },
    methods: {
      ...mapMutations('mycart', ['updateAllSelected']),
      ...mapMutations('myUser', ['updateRedirectInfo']),
      controlRadio() {
        this.updateAllSelected(!this.isAllSelected)
      },
      settelment() {
        if (!this.getChecked) return uni.$showMsg('请选择要结算的商品！')
        if (!this.addressStr) return uni.$showMsg('请选择地址！')
        // if(!this.token) return uni.$showMsg('请先登录！')
        if (!this.token) return this.gotoLogin()

        this.payOrder()
      },
      gotoLogin() {
        this.seconds = 3
        this.showtips(this.seconds)
        this.timer = setInterval(() => {
          this.seconds--
          if (this.seconds < 1) {
            clearInterval(this.timer)
            uni.switchTab({
              url: '../../pages/my/my',
              success: () => {
                this.updateRedirectInfo({
                  // 跳转的方式
                  openType: 'switchTab',
                  // 从哪个页面跳转过去的
                  from: '/pages/cart/cart'
                })
              }
            })
            return
          }
          // console.log(this.seconds)
          this.showtips(this.seconds)
        }, 1000)

      },
      showtips(n) {
        uni.showToast({
          // 不展示任何图标
          icon: 'none',
          // 提示的消息
          title: '请登录后再结算！' + n + ' 秒后自动跳转到登录页',
          // 为页面添加透明遮罩，防止点击穿透
          mask: true,
          // 1.5 秒后自动消失
          duration: 1500
        })
      },
      async payOrder() {
        // 创建订单
        const orderInfo = {
          // order_price:this.getGoodsPrice
          order_price: 0.01,
          consignee_addr: this.addressStr,
          goods: this.cart.filter(x => x.goods_state).map(x => ({
            goods_id: x.goods_id,
            goods_number: x.goods_count,
            goods_price: x.goods_price
          }))
        }
        const {
          data: res
        } = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
        // console.log(res.meta.status)
        if (res.meta.status !== 200) return uni.$showMsg('创建订单失败')
        const orderNumber = res.message.order_number
        // console.log(orderNumber)
        // 2.订单预支付
        const {
          data: result
        } = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {
          order_number: orderNumber
        })
        if (result.meta.status !== 200) return uni.$showMsg('预付订单生成失败！')
        // console.log(result)
        const payInfo = result.message.pay
        console.log(payInfo)
        // 3. 发起微信支付
        const [err, succ] = await uni.requestPayment(payInfo)
        if (err) return uni.$showMsg('订单未支付')
        const {
          data: res3
        } = await uni.$http('/api/public/v1/my/orders/chkOrder', {
          order_number: orderNumber
        })
        // console.log(res3)
        if (res3.meta.status !== 200) return uni.$showMsg('订单未支付！')
        uni.showToast({
          title: '支付完成！',
          icon: 'success'
        })
      }

    }
  }
</script>

<style lang="scss">
  .my-settle-container {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 50px;
    // 将背景色从 cyan 改为 white
    background-color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 5px;
    font-size: 14px;

    .radio {
      display: flex;
      align-items: center;
    }

    .amount {
      color: #f36414;
    }

    .btn-settle {
      height: 50px;
      min-width: 100px;
      background-color: #f36414;
      color: white;
      line-height: 50px;
      text-align: center;
      padding: 0 10px;
    }
  }
</style>
